package com.ruoyi.factorydata.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.factorydata.domain.WorkOrderInfo;
import com.ruoyi.factorydata.domain.WorkOrderLog;
import com.ruoyi.factorydata.mapper.WorkOrderLogMapper;
import com.ruoyi.factorydata.service.IWorkOrderLogService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * 工单生产记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-31
 */
@Service
public class WorkOrderLogServiceImpl extends ServiceImpl<WorkOrderLogMapper, WorkOrderLog> implements IWorkOrderLogService {
    private final WorkOrderLogMapper workOrderLogMapper;
    private final ISysUserService sysUserService;

    public WorkOrderLogServiceImpl(WorkOrderLogMapper workOrderLogMapper, ISysUserService sysUserService) {
        this.workOrderLogMapper = workOrderLogMapper;
        this.sysUserService = sysUserService;
    }

    /**
     * 查询工单生产记录
     *
     * @param id 工单生产记录主键
     * @return 工单生产记录
     */
    @Override
    public WorkOrderLog selectWorkOrderLogById(Long id) {
        return workOrderLogMapper.selectWorkOrderLogById(id);
    }

    /**
     * 查询工单生产记录列表
     *
     * @param workOrderLog 工单生产记录
     * @return 工单生产记录
     */
    @Override
    public List<WorkOrderLog> selectWorkOrderLogList(WorkOrderLog workOrderLog) {
        List<WorkOrderLog> workOrderLogs = workOrderLogMapper.selectWorkOrderLogList(workOrderLog);
        HashMap<Long, String> userNameCache = new HashMap<>();
        for (WorkOrderLog log : workOrderLogs) {
            log.setCreateByName(sysUserService.getUserNameByIdAndCache(Long.parseLong(log.getCreateBy()), userNameCache));
        }
        return workOrderLogs;
    }

    /**
     * 新增工单生产记录
     *
     * @param workOrderLog 工单生产记录
     * @return 结果
     */
    @Override
    public int insertWorkOrderLog(WorkOrderLog workOrderLog) {
                workOrderLog.setCreateTime(DateUtils.getNowDate());
            return workOrderLogMapper.insertWorkOrderLog(workOrderLog);
    }

    /**
     * 修改工单生产记录
     *
     * @param workOrderLog 工单生产记录
     * @return 结果
     */
    @Override
    public int updateWorkOrderLog(WorkOrderLog workOrderLog) {
                workOrderLog.setUpdateTime(DateUtils.getNowDate());
        return workOrderLogMapper.updateWorkOrderLog(workOrderLog);
    }

    /**
     * 批量删除工单生产记录
     *
     * @param ids 需要删除的工单生产记录主键
     * @return 结果
     */
    @Override
    public int deleteWorkOrderLogByIds(Long[] ids) {
        return workOrderLogMapper.deleteWorkOrderLogByIds(ids);
    }

    /**
     * 删除工单生产记录信息
     *
     * @param id 工单生产记录主键
     * @return 结果
     */
    @Override
    public int deleteWorkOrderLogById(Long id) {
        return workOrderLogMapper.deleteWorkOrderLogById(id);
    }

    @Override
    public void saveLog(WorkOrderInfo workOrderInfo, String content) {
        WorkOrderLog orderLog = new WorkOrderLog();
        orderLog.setProjectId(workOrderInfo.getProjectId());
        orderLog.setProjectCode(workOrderInfo.getProjectCode());
        orderLog.setPlanId(workOrderInfo.getPlanId());
        orderLog.setPlanCode(workOrderInfo.getPlanCode());
        orderLog.setWorkOrderId(workOrderInfo.getId());
        orderLog.setWorkOrderCode(workOrderInfo.getWorkOrderCode());
        orderLog.setOptContent(content);
        orderLog.setCreateBy(SecurityUtils.getUserId() + "");
        orderLog.setCreateTime(DateUtils.getNowDate());
        workOrderLogMapper.insertWorkOrderLog(orderLog);
    }
}